package com.bxait.toymail.ware.config;

import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.core.*;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/*
@description:
@ClassName MyRabbitConfig
@author Chen
@create 2023-06-18 15:32
@Version 1.0
*/
@Configuration
public class MyRabbitConfig {
    /*
     * @param 
     * @==============History===============<br/>
     * @Description //   使用json序列化在MQ中进行传输
     * @Date 2023/6/18 2023/6/18 
     * @Author < a href="2277411582@qq.com">chen</ a>
     * @return ch.qos.logback.classic.pattern.MessageConverter
     */
    @Bean
    public MessageConverter messageConverter(){
        return new Jackson2JsonMessageConverter();
    }

    /*
     * @param 
     * @==============History===============<br/>
     * @Description //   创建一个交换机  stock-event-exchange
     * @Date 2023/6/18 2023/6/18 
     * @Author < a href="2277411582@qq.com">chen</ a>
     * @return org.springframework.amqp.core.Exchange
     */
    @Bean
    public Exchange stockEventExchange(){
        return  new TopicExchange("stock-event-exchange",true,false);
    }

    /*
     * @param
     * @==============History===============<br/>
     * @Description //   创建一个队列,这个队列就是一个普通队列,拿到消息就会消费  stock.release.stock.queue
     *                   说白了饶了一圈,我们的业务逻辑最终就是最终要消费这个队列中的消息
     * @Date 2023/6/18 2023/6/18
     * @Author < a href="2277411582@qq.com">chen</ a>
     * @return org.springframework.amqp.core.Queue
     */
    @Bean
    public Queue stockReleaseStockQueue(){
        return new Queue("stock.release.stock.queue",true,false,false);
    }

    /*
     * @param
     * @==============History===============<br/>
     * @Description //   这个一个特殊的队列,是一个没有消费者消费的队列,设置消息的过期时间为120000毫秒,
     *                           到期后会使用stock.release的路由健将消息在转发给stock-event-exchange交换机。所以这是一个延迟队列
     * @Date 2023/6/18 2023/6/18
     * @Author < a href="2277411582@qq.com">chen</ a>
     * @return org.springframework.amqp.core.Queue
     */
    @Bean
    public Queue stockDelayQueue(){
        Map<String,Object> args = new HashMap<>();
        args.put("x-dead-letter-exchange","stock-event-exchange");
        args.put("x-dead-letter-routing-key","stock.release");
        args.put("x-message-ttl",120000);
        return new Queue("stock.delay.queue",true,false,false,args);
    }


    /*
     * @param 
     * @==============History===============<br/>
     * @Description //   这是一个绑定关系,将这个普通队列与stock-event-exchange交换机绑定使用stock.release.#的路由健
     * @Date 2023/6/18 2023/6/18 
     * @Author < a href="2277411582@qq.com">chen</ a>
     * @return org.springframework.amqp.core.Binding
     */
    @Bean
    public Binding stockReleaseBinding(){
        return  new Binding("stock.release.stock.queue",
                Binding.DestinationType.QUEUE,
                "stock-event-exchange",
                "stock.release.#",
                null);
    }

    /*
     * @param
     * @==============History===============<br/>
     * @Description //   这是一个绑定关系,将这个延时队列与stock-event-exchange进行绑定,使用stock.locked这个路由健
     * @Date 2023/6/18 2023/6/18
     * @Author < a href="2277411582@qq.com">chen</ a>
     * @return org.springframework.amqp.core.Binding
     */
    @Bean
    public Binding stockLockedBinding(){
        return  new Binding("stock.delay.queue",
                Binding.DestinationType.QUEUE,
                "stock-event-exchange",
                "stock.locked",
                null);
    }

}
